﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using Dycox.ComponentModel;
using Dycox.Validation;
using Newtonsoft.Json;

namespace Dycox.Workflows
{
    [DataContract]
    public class StageContract  : OperationContract
    {

        public StageContract()
        {

        }

        public virtual void Apply(OperationArguments args, WFStep step, Workflow workflow, bool modifying)
        {
        }

        protected void CopyProperties(object target, params ModelPropertyUsage[] usages)
        {
            if (target != null)
            {
                PropertyDescriptorCollection pdct = TypeDescriptor.GetProperties(target);

                foreach (ModelPropertyUsage u in usages)
                {
                    PropertyDescriptorCollection pdcs = TypeDescriptor.GetProperties(this, new Attribute[] { ModelPropertyAttribute.GetInstance(u) });
                    foreach (PropertyDescriptor pds in pdcs)
                    {
                        PropertyDescriptor pdt = pdct[pds.Name];
                        if (pdt != null)
                            pdt.SetValue(target, pds.GetValue(this));
                    }

                }
            }
        }

        public virtual void Undo(WorkflowUndoContext context)
        {
            //IReflectionRoot appObj = context.AppObject;

            //if (appObj != null)
            //    context.RestoreOriginalState(appObj);
            context.Restore();
        }

        internal protected virtual void InitializeNew(OperationArguments args, Stage stage)
        {
        }

        private string _userAction;

        [
        DataMember(EmitDefaultValue = false),
        DefaultValue(null),
        ModelProperty(ModelPropertyUsage.Control, false),
        Browsable(false)
        ]
        public string UserAction
        {
            get { return _userAction; }
            set
            {
                if (_userAction != value)
                {
                    _userAction = value;
                    SendPropertyChanged("UserAction");
                }

            }
        }

    }
}
